<template>
  <div>
    <div class="from">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <h2>账号注册</h2>

        <!-- 账号名称 -->
        <el-form-item label="账号名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <!-- 账号密码 -->
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- 店铺名称 -->
        <el-form-item label="店铺名称" prop="shopname">
          <el-input v-model="ruleForm.shopname"></el-input>
        </el-form-item>
        <!-- 店铺地址 -->
        <el-form-item label="店铺地址" prop="address">
          <el-input v-model="ruleForm.address" type="textarea"></el-input>
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email" type="email"></el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item label="验证码" prop="code" class="code">
          <el-input v-model="ruleForm.code" class="c-input"></el-input>
          <el-button type="primary" plain @click="getCode()" :disabled="flag">{{
            flag ? time + "秒后获取" : "获取验证码"
          }}</el-button>
        </el-form-item>
        <!-- 提交 -->
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getCodeNum, apply, applys } from "@/api/register";
export default {
  data() {
    //   两次密码校验规则
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        name: "",
        pass: "",
        checkPass: "",
        email: "",
        shopname: "",
        address: "",
        code: "",
      },
      time: 6,
      flag: false,
      timer: null,
      rules: {
        name: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
        shopname: [
          { required: true, message: "请输入店铺名称", trigger: "blur" },
        ],
        address: [
          { required: true, message: "请输入店铺地址", trigger: "blur" },
        ],
        pass: [{ required: true, validator: validatePass, trigger: "blur" }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    //   提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 发送注册请求
          applys({
            account: this.ruleForm.name,
            pw: this.ruleForm.pass,
            shopname: this.ruleForm.shopname,
            address: this.ruleForm.address,
            email: this.ruleForm.email,
            code: this.ruleForm.code,
          })
            .then((res) => {
              console.log("PPPP");
              //判断成功
              if ("success" === res.data.status) {
                // 提示成功
                this.$message({
                  message: res.data.msg,
                  type: "success",
                });
                // 跳转登录
                this.$router.replace("/login");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 获取验证码
    getCode() {
      this.$refs["ruleForm"].validateField("email", (valid) => {
        //判断邮箱
        if (!valid) {
          //设置不可点击获取验证码
          this.flag = true;
          //   发起获取验证码请求
          this.timer = setInterval(() => {
            console.log(this.time);
            //时间减1
            this.time--;
            //判断时间边界
            if (this.time <= 0) {
              // 重置时间
              this.time = 6;
              //开启按键
              this.flag = false;
              clearInterval(this.timer);
            }
          }, 1000);
          getCodeNum({ email: this.ruleForm.email }).then((res) => {
            console.log(res);
          });
        }
      });
    },
  },
  beforeDestroy() {
    //   销毁定时器
    clearInterval(timer);
  },
};
</script>

<style lang="scss" scoped>
.from {
  width: 500px;
  margin: 0 auto;
  padding-top: 30px;
  box-sizing: border-box;
  //   验证码
  .code {
    .c-input {
      width: 100px;
      margin-right: 20px;
    }
  }
}
</style>